<?
$conn=false;
function connectToDB() {
	global $conn;
	if(!$conn) {
		$conn = mysql_connect('localhost', 'root', '*****');
		mysql_select_db("mc2",$conn);
	}
	return $conn;
}

function getGidByName($name) {
	connectToDB();
	$data = mysql_fetch_array(mysql_query("SELECT `people_group_id` FROM `people_group` WHERE `people_group_name`='" . mysql_real_escape_string($name) . "'"));
	return $data['people_group_id'];
}

function pageId2Name($pagename) {
	connectToDB();
	$query="SELECT page.page_name FROM page WHERE page.page_id='" . mysql_real_escape_string($pagename) . "'";
	$data = mysql_query($query);
	if(mysql_num_rows($data)<1) {
		return false;
	}
	$data = mysql_fetch_array($data);
	return $data['page_name'];
}

function pageName2Id($pagename) {
	connectToDB();
	$query="SELECT page.page_id FROM page WHERE page.page_name='" . mysql_real_escape_string($pagename) . "'";
	$data = mysql_query($query);
	if(mysql_num_rows($data)<1) {
		return false;
	}
	$data = mysql_fetch_array($data);
	return $data['page_id'];
}

function pageScript2Id($pagename) {
	connectToDB();
	$query="SELECT page.page_id FROM page WHERE page.page_script='" . mysql_real_escape_string($pagename) . "'";
	$data = mysql_query($query);
	if(mysql_num_rows($data)<1) {
		return false;
	}
	$data = mysql_fetch_array($data);
	return $data['page_id'];
}

function getGroupList($all=false) {
	$query="SELECT * FROM `people_group`";
	$newdata = mysql_query($query);
	$awesome=array();
	while($data = mysql_fetch_array($newdata)) {
		if(($all) AND isSystemGroup($data['people_group_id'])) {
			$awesome[$data['people_group_name']]=$data;
		}
		if(!isSystemGroup($data['people_group_id'])) {
			$awesome[$data['people_group_name']]=$data;
		}
	}
	return $awesome;
}

function getStudentList() {
	connectToDB();
	$rawpeople=mysql_query("SELECT people.* FROM `people` JOIN `people_in_group` ON people_in_group.people_id = people.people_id JOIN `people_group` ON people_group.people_group_id=people_in_group.people_group_id WHERE people.people_active='1' AND people_group.people_group_name='Students' ORDER BY people.people_lastname");
	echo mysql_error();
	$array=array();
	for($i=0;$people=mysql_fetch_array($rawpeople);$i++) {
		$array[$i]=$people;
	}
	return $array;
}

function customError($msg) {
	echo $msg;
	echo "<br>Please post this error message along with EXACTLY what you were doing when this happened in the <a href=\"../mantis/\" target=\"_blank\">bug tracker</a>! Thanks!";
	echo "\n<a href=\"/home.php\">Click here to go back to your home page</a>";
	die;
	
	
}
function currentPage() {
	connectToDB();
	$data=mysql_query("SELECT * FROM `page` WHERE `page_script`='" . mysql_real_escape_string($_SERVER['SCRIPT_NAME']) . "'");
	return mysql_fetch_array($data);
}

function groupExistsName($groupname) {
	connectToDB();
	$rpeople_group=mysql_query("SELECT * FROM `people_group` WHERE `people_group_name`='" . mysql_real_escape_string($group) . "'");
	if(mysql_num_rows($rpeople_group)!=1) {
		return false;
	}
	return true;
}

function groupExistsId($id) {
	connectToDB();
	$rpeople_group=mysql_query("SELECT * FROM `people_group` WHERE `people_group_id`='" . mysql_real_escape_string($id) . "'");
	if(mysql_num_rows($rpeople_group)!=1) {
		return false;
	}
	return true;
}

function isSystemGroup($gid) {
	connectToDB();
	if(!is_numeric($gid)) {
		$gid=getGidByName($gid);
	}
	if(!is_numeric($gid)) {
		return false;
	}
	$data=mysql_query("SELECT * FROM people_group WHERE people_group_id='" . mysql_real_escape_string($gid) . "' AND `people_group_system`='1'");
	return (mysql_num_rows($data)==1 ? true : false);
}

function groupInfo($id) {
	connectToDB();
	if(groupExistsId($id)) {
		$rpeople_group=mysql_query("SELECT * FROM `people_group` WHERE `people_group_id`='" . mysql_real_escape_string($id) . "'");
		return mysql_fetch_array($rpeople_group);
	} else {
		return false;
	}
}
/*
		function getGroupList($special="") {
	connectToDB();
	$rpeople_group=mysql_query("SELECT * FROM `people_group` " . $special);
	$array=array();
	while($people_group=mysql_fetch_array($rpeople_group)) {
	$array[$people_group['people_group_id']]=$people_group;
}
	return $array;
}
*/
function getUserList($special="none",$showDisabled=0) {
	connectToDB();
	if($special="none") {
		$special=" ORDER BY people.people_lastname";
	}
	$rawpeople=mysql_query("SELECT people.* FROM `people` " . $special);
	$array=array();
	for($i=0;$people=mysql_fetch_array($rawpeople);$i++) {
		if(($people['people_active']==1) OR ($showDisabled==1)) {
			$array[$i]=$people;
		} else { $i--; }
	}
	return $array;
}

function getAdvisoryList($special="") {
	connectToDB();
	$rawpeople=mysql_query("SELECT advisory.* FROM `advisory` " . $special);
	$array=array();
	while($people=mysql_fetch_array($rawpeople)) {
		$array[$people['advisory_id']]=$people;
	}
	return $array;
}

function getPageList($special="") {
	connectToDB();
	$rawpeople=mysql_query("SELECT page.* FROM `page`" . $special);
	$array=array();
	while($people=mysql_fetch_array($rawpeople)) {
		$array[$people['page_id']]=$people;
	}
	return $array;
}

function isStudent($userid) {
	connectToDB();
	$student=new Person($userid);
	return $student->isInGroup("Students");
}

function isAdvisor($userid) {
	connectToDB();
	$student=new Person($userid);
	return $student->isInGroup("Advisors");
}

function studentIsIn($studentid,$day="none") {
	if($day=="none") {
		$day=$day=unixToDate(time());
	}
	connectToDB();
	$rawdata=mysql_query("SELECT * FROM attendance WHERE DATE(`attendance_in`)='" . $day . "' AND DATE(`attendance_out`)!='" . $day . "' AND `people_id`='" . mysql_real_escape_string($studentid) . "'");
	return (mysql_num_rows($rawdata)>0 ? true : false);
}

function studentIn($personid,$timein,$day="notset") {
	if($day=="notset") {
		$day=unixToDate($timein);
	}
	connectToDB();
	if(studentIsIn($personid,$day)) {
		return false;
	}
	mysql_query("INSERT INTO `attendance` (`people_id`,`attendance_in`,`attendance_out`) VALUES ('" . mysql_real_escape_string($personid) . "',FROM_UNIXTIME(" . $timein . "),'0')");
	echo mysql_error();
	return mysql_insert_id();
}

function schoolStart($day="notset") {
	if($day=="notset") {
		$day=unixToDate(time());
	}
	connectToDB();
	$rawthing=mysql_query("SELECT attendance_special_start FROM `attendance_special` WHERE DATE(attendance_special_start)=DATE('" . $day . "')");
	echo mysql_error();
	if(mysql_num_rows($rawthing)<1) {
		$rawtimein=mysql_query("SELECT attendance_in FROM `attendance` WHERE TIME(`attendance_in`)!='00:00:00' AND `attendance_excused`='1' AND DATE(`attendance_in`)=DATE('" . $day . "')");
		$times=array();
		while($timein=mysql_fetch_array($rawtimein)) {
			if(!isset($times[$timein['attendance_in']])) {
				$times[$timein['attendance_in']]=0;
			}
			$times[$timein['attendance_in']]++;
		}
		$max=0;
		foreach($times as $key=>$value) {
			if($value>$max) {
				$starttime=$key;
				$max=$value;
			}
		}
		echo $max;
		if($max>10) {
	//date("Y",timestampToUnix($day)) . "-" . date("m",timestampToUnix($day)) . "-" . date("d",timestampToUnix($day)) . " " . 
			$schoolstart=timestampToUnix($starttime);
		} else {
			$schoolstart=timestampToUnix(date("Y",timestampToUnix($day)) . "-" . date("m",timestampToUnix($day)) . "-" . date("d",timestampToUnix($day)) . " 08:00:00");
		}
		mysql_query("INSERT INTO `attendance_special` (`attendance_special_start`) VALUES(FROM_UNIXTIME('" . mysql_real_escape_string($schoolstart) . "'))");
		echo mysql_error();
		return $schoolstart;
	}
	$thing=mysql_fetch_array($rawthing);
	return timestampToUnix($thing[0]);
}



function schoolEnd() {
	connectToDB();
	$rawthing=mysql_query("SELECT UNIX_TIMESTAMP(attendance_special_end) FROM `attendance_special` WHERE DATE(attendance_special_end)=CURRENT_DATE()");
	if(mysql_num_rows($rawthing)<1) {
		$awesome = mysql_fetch_array(mysql_query("SELECT UNIX_TIMESTAMP(" . date("Y") . "-" . date("i") . "-" . date("d") . " 15:15:00)"));
		return $awesome[0];
	}
	$thing=mysql_fetch_array($rawthing);
	return $thing[0];
}

function studentOut($personid,$timeout) {
	if(!studentIsIn($personid,unixToDate($timeout))) {
		return false;
	}
	mysql_query("UPDATE `attendance` SET `attendance_out`=FROM_UNIXTIME('" . mysql_real_escape_string($timeout) . "') WHERE DATE(`attendance_in`)='" . unixToDate($timeout) . "' AND DATE(`attendance_out`)!='" . unixToDate($timeout) . "' AND `people_id`='" . mysql_real_escape_string($personid) . "'");
}

function HMStoUnix($hours,$minutes,$seconds=0,$day="none") {
	if($day=="none") {
		$day=unixToDate(time());
	}
	connectToDB();
	$time=mysql_query("SELECT UNIX_TIMESTAMP('" . date("Y-m-d",timestampToUnix($day)) . " " . str_pad($hours,2,"0",STR_PAD_LEFT) . ":" . str_pad($minutes,2,"0",STR_PAD_LEFT) . ":" . str_pad($seconds,2,"0",STR_PAD_LEFT) . "')");
	echo mysql_error();
	$time=mysql_fetch_array($time);
	return $time[0];
}

function getInOut($uid,$day="none",$dbg=0) {
	if($day=="none") {
		$day=unixToDate(time());
	}
	connectToDB();
	$things=mysql_query("SELECT * FROM `attendance` WHERE `people_id`='" . mysql_real_escape_string($uid) . "' AND DATE(attendance_in)=DATE('" . mysql_real_escape_string($day) . "') ORDER BY `attendance_in`");
	echo mysql_error();
	$sreturn=array();
	$i=0;
	while($data=mysql_fetch_array($things)) {
		$sreturn[$i]=$data;
		$i++;
	}
	if($dbg==1) {
		echo mysql_num_rows($things);
		echo "Before Return:";
		print_r($sreturn);
	}
	return $sreturn;
}

function getAttendance($uid,$day) {
	connectToDB();
	$inout=getInOut($uid,$day,0);
	if(!$inout) {
		return "absent:unexcused";
	}
	if($inout[0]['attendance_out']!="0000-00-00 00:00:00") {
		return "dismiss:" . (($inout[0]['attendance_excused']==1)?"excused":"unexcused");
	}
	if($inout[0]['attendance_in']==unixToTimestamp(HMStoUnix(0,0,0,$day))) {
		return "absent:" . (($inout[0]['attendance_excused']==1)?"excused":"unexcused");
	}
	if($inout[0]['attendance_in']==unixToTimestamp(schoolStart($day))) {
		return "present:excused";
	}
	return "tardy:" . (($inout[0]['attendance_excused']==1)?"excused":"unexcused");
}

function noSchool($day) {
	$users=getUserList();
	$yes=0;
	$no=0;
	foreach($users as $user) {
		if(isStudent($user['people_id'])) {
			if(!getInOut($user['people_id'],$day)) {
				$no++;
			} else {
				$yes++;
			}
		}
	}
	if(($no/($no+$yes))>.75) {
		return true;
	}
	return false;
}

function dayOfWeek($timestamp) {
	connectToDB();
	if(is_numeric($timestamp)) {
		$timestamp=unixToTimestamp($timestamp);
	}
	$day=mysql_fetch_array(mysql_query("SELECT DAYNAME('" . mysql_real_escape_string($timestamp) . "')"));
	echo mysql_error();
	return $day[0];
}

function timestampToUnix($timestamp) {
	connectToDB();
	$awesome=mysql_fetch_array(mysql_query("SELECT UNIX_TIMESTAMP('" . mysql_real_escape_string($timestamp) . "')"));
	return $awesome[0];
}

function unixToTimestamp($timestamp) {
	connectToDB();
	$awesome=mysql_fetch_array(mysql_query("SELECT FROM_UNIXTIME('" . mysql_real_escape_string($timestamp) . "')"));
	return $awesome[0];
}

function unixToDate($timestamp) {
	if(!is_numeric($timestamp)) {
		return timestampToDate($timestamp);
	}
	connectToDB();
	$awesome=mysql_fetch_array(mysql_query("SELECT DATE(FROM_UNIXTIME('" . mysql_real_escape_string($timestamp) . "'))"));
	return $awesome[0];
}

function timestampToDate($timestamp) {
	if(is_numeric($timestamp)) {
		return unixToDate($timestamp);
	}
	connectToDB();
	$awesome=mysql_fetch_array(mysql_query("SELECT DATE('" . mysql_real_escape_string($timestamp) . "')"));
	return $awesome[0];
}

function deleteInOut($id) {
	connectToDB();
	mysql_query("DELETE FROM `attendance` WHERE `attendance_id`='" . mysql_real_escape_string($id) . "'");
}

function getTardies($from,$to,$uid) {
	connectToDB();
	$awesome=mysql_query("SELECT * FROM `attendance` WHERE `people_id`='" . mysql_real_escape_string($uid) . "' AND DATE(attendance_in)>DATE(FROM_UNIXTIME('" . mysql_real_escape_string($from) . "')) AND DATE(attendance_in)<DATE(FROM_UNIXTIME('" . mysql_real_escape_string($to) . "'))");
	$return=array();
	while($data=mysql_fetch_array($awesome)) {
		$return[$data['attendance_id']]=$data;
	}
	return $return;
}

function setExcused($id,$excused,$comments="") {
	connectToDB();
	mysql_query("UPDATE `attendance` SET `attendance_excused`='" . mysql_real_escape_string($excused) . "', `attendance_comments`='" . mysql_real_escape_string($comments) . "' WHERE `attendance_id`='" . mysql_real_escape_string($id) . "'");
}

function advisoryInfo($aid) {
	connectToDB();
	$awesome=mysql_query("SELECT * FROM `advisory` WHERE `advisory_id`='" . mysql_real_escape_string($aid) . "'");
	if(!$awesome) {
		return false;
	}
	if(mysql_num_rows($awesome)!=1) {
		return false;
	}
	$awesome=mysql_fetch_array($awesome);
	return $awesome;
}

function getStatusByName($name) {
	connectToDB();
	$awesome=mysql_query("SELECT * FROM status WHERE `status_value`=\"" . mysql_real_escape_string($name) . "\"");
	$awesome=mysql_fetch_array($awesome);
	return $awesome['status_id'];
}

function deleteUser($uid) {
	connectToDB();
	$uid=mysql_real_escape_string($uid);
	$people_array=array(
		"access_values",
		"advisory_linker",
		"attendance",
		"calendar_people",
		"checklist_value",
		"ilp"=>array(
			"ilp_checklist",
			"ilp_field_value"
			),
		"exhibition"=>array(
			"exhibition_ilp",
			"exhibition_final_assessment",
			"exhibition_attendee"=>array(
				"exhibition_assessment",
				"exhibition_comment",
				"exhibition_ind_assessment"
				)
			),
		"video_group"=>array(
			"video_linker"
			)
		);
	return deleteTableChildren("people","WHERE `people_id`='" . $uid . "' LIMIT 1",$people_array);
}

function deleteTableChildren($tablename,$where,$childlist) {
	connectToDB();
	$rawtable = mysql_query("SELECT * FROM `" . mysql_real_escape_string($tablename) . "` " . $where);
	if(mysql_num_rows($rawtable) < 0) {
		return false;
	}
	if(mysql_num_rows($rawtable) > 1) {
		while(mysql_fetch_array($rawtable)) {
			return deleteTableChildren($tablename,"WHERE `" . mysql_real_escape_string($tablename) . "_id`='" . $table[$tablename."_id"] . "'",$childlist);
		}
	}
	if(mysql_num_rows($rawtable)==0) {
		return true;
	}
	$table = mysql_fetch_array($rawtable);
	foreach($childlist as $parent=>$child) {
		if(is_array($child)) {
			if(!deleteTableChildren($parent,"WHERE `" . mysql_real_escape_string($tablename) . "_id`='" . $table[$tablename."_id"] . "'",$child)) {
				return false;
			}
		} else {
			mysql_query("DELETE FROM `" . mysql_real_escape_string($child) . "` WHERE `" . mysql_real_escape_string($tablename) . "_id`='" . $table[$tablename."_id"] . "'");
		}
	}
	mysql_query("DELETE FROM `" . mysql_real_escape_string($tablename) . "` WHERE `" . mysql_real_escape_string($tablename) . "_id`='" . $table[$tablename."_id"] . "'");
	return true;
}

function getChecklistList() {
	connectToDB();
	$checks=mysql_query("SELECT * FROM `checklist`");
	$array=array();
	for($i=0;$check=mysql_fetch_array($checks);$i++) {
		$array[$i]=$check;
	}
	return $array;
}

function getChecklistCatagories() {
	connectToDB();
	$rawcata=mysql_query("SELECT * FROM `checklist_group`");
	$array=array();
	while($cata=mysql_fetch_array($rawcata)) {
		$array[$cata['checklist_group_name']]=array();
		$array[$cata['checklist_group_name']]['id']=$cata['checklist_group_id'];
		$rawchecks=mysql_query("SELECT * FROM checklist_item");
		while($checks=mysql_fetch_array($rawchecks)) {
		}
	}
}

function getAssessmentTypeInfo($assid=1) {
	connectToDB();
	$rawdata=mysql_query("SELECT * FROM `assessment_type` WHERE `assessment_type_id`='" . mysql_real_escape_string($assid) . "'");
	if(mysql_num_rows($rawdata)!=1) {
		customError("getAssessmentTypeInfo rawdata return " . mysql_num_rows($rawdata) . "rows" . mysql_error());
		return false;
	}
	$assessment=mysql_fetch_array($rawdata);
	$rawforms = mysql_query("SELECT * FROM `assessment_field` WHERE `assessment_type_id`='" . mysql_real_escape_string($assid) . "'");
	$assessment['forms']=array();
	while($form=mysql_fetch_array($rawforms)) {
		$assessment['forms'][$form['assessment_field_id']]=$form;
	}
	return $assessment;
}

function showForm($form,$value="") {
	connectToDB();
	$field=mysql_fetch_array(mysql_query("SELECT * FROM `field_type` WHERE `field_type_id`='" . mysql_real_escape_string($form['field_type_id']) . "'"));
	$ret="";
	$name=$form['field_name'];
	switch($field['field_type_display']) {
		case "text":
			return "<input type=\"text\" name=\"" . $form['field_name'] . "\" value=\"$value\">";
			break;
		case "password":
			return "<input type=\"password\" name=\"" . $form['field_name'] . "\" value=\"$value\">";
			break;
		case "textarea":
			return "<textarea name=\"" . $form['field_name'] . "\">$value</textarea>";
			break;
	}
	return "Invalid field type: " . $field['field_type_display'];
}
		
function getLevels() {
	connectToDB();
	$raw=mysql_query("SELECT * FROM `level` ORDER BY `level_level`");
	echo mysql_error();
	$array=array();
	while($indeed=mysql_fetch_array($raw)) {
			$array[$indeed['level_id']]=$indeed;
	}
	return $array;
}

function getHabits($full=false) {
	connectToDB();
	$levels=getLevels();
	$raw=mysql_query("SELECT * FROM `habit`");
	$habits=array();
	$ind=array();
	$habnum=0;
	while($habit=mysql_fetch_array($raw)) {
		$raw2=mysql_query("SELECT * FROM `habit_indicator` WHERE `habit_id`='" . $habit['habit_id'] . "'");
		if($full) {
			$habits[$habnum]['name']=$habit['habit_name'];
			$habits[$habnum]['id']=$habit['habit_id'];
		}
		$indnum=0;
		while($ind=mysql_fetch_array($raw2)) {
			if($full) {
				$habits[$habnum][$indnum]['name']=$ind['habit_indicator_name'];
				$habits[$habnum][$indnum]['id']=$ind['habit_indicator_id'];
			}
			$levnum=0;
			foreach($levels as $level) {
				$desc=mysql_fetch_array(mysql_query("SELECT * FROM `habit_description` WHERE `habit_indicator_id`='" . mysql_real_escape_string($ind['habit_indicator_id']) . "' AND `level_id`='" . $level['level_id'] . "'"));
				$habits[$habit['habit_name']][$ind['habit_indicator_name']][$level['level_name']]=$desc;
				if($full) {
					$habits[$habnum][$indnum][$levnum]=$desc;
				}
				$levnum++;
			}
			$indnum++;
		}
		$habnum++;
	}
	return $habits;
}

function newAssessment($typeid,$people_id) {
	connectToDB();
	mysql_query("INSERT INTO `assessment` (`assessment_type_id`,`people_id`,`assessment_date` ) VALUES ( '" . $typeid . "','" . $people_id . "', NOW())");
	return mysql_insert_id();
}

function insertAssessmentData($typeid,$people_id,$data) {
	updateAssessment(newAssessment($typeid,$people_id),$data);
}

function updateAssessment($assid,$data) {
	connectToDB();
	mysql_query("DELETE FROM `assessment_field_value` WHERE `assessment_id`='" . mysql_real_escape_string($assid) . "'");
	mysql_query("DELETE FROM `assessment_habit` WHERE `assessment_id`='" . mysql_real_escape_string($assid) . "'");
	mysql_query("UPDATE `assessment` SET `assessment_name`='" . mysql_real_escape_string($data['name']) . "' WHERE `assessment_id`='" . mysql_real_escape_string($assid) . "'");
	$habits=getHabits(true);
	$levels=getLevels();
	$i=1;
	$rlevels=array();
	foreach($levels as $id=>$lev) {
		$rlevels[$i]=$lev;
		$rlevels["id".$i]=$id;
		$i++;
	}
// 	var_dump($rlevels);
	foreach($data as $key=>$val) {
		if($key!="habits" AND $key!="name") {
			$thing=mysql_fetch_array(mysql_query("SELECT * FROM `assessment_field` WHERE `field_name`='" . mysql_real_escape_string($key) . "'"));
			mysql_query("INSERT INTO `assessment_field_value` (`assessment_id`, `assessment_field_value`, `assessment_field_id`) VALUES ('" . $assid . "', '" . mysql_real_escape_string($val) . "', '" . $thing['assessment_field_id'] . "')");
		} elseif($key=="habits") {
			foreach($val as $habit=>$ind_a) {
				foreach($ind_a as $ind=>$level) {
			//*
// 					echo $level."\n";
					mysql_query/*/print("<br>\n" . /*/(/* */"INSERT INTO `assessment_habit` (`assessment_id`,`habit_indicator_id`,`level_id`,`level_plus`) VALUES ('" . $assid . "','" . $habits[$habit][$ind]['id'] . "','" . $rlevels[(floor($level))]['level_id'] . "','" . (($level-floor($level))*2) . "')");
				}
			}
		}
	}
}

function toHabitData($data) {
	connectToDB();
	$levels=getLevels();
	$i=1;
	$rlevels=array();
	$habitdata=array();
	$ind_data=array();
	$thing=mysql_query("SELECT habit_id,habit_indicator_id FROM habit_indicator");
	$habits=getHabits(true);
	$idmap=array();
	echo "<pre>";
	foreach($habits as $habitnum=>$habit_d) {
// 		var_dump($habit_d);
		foreach($habit_d as $indnum=>$ind_d) {
	if(is_array($ind_d) && is_numeric($indnum)) {
				$idmap[$habit_d['id']][$ind_d['id']]['habit_num']=$habitnum;
				$idmap[$habit_d['id']][$ind_d['id']]['ind_num']=$indnum;
				$habitdata[$habitnum][$indnum]=-1;
			}
		}
	}
	while($h_ind=mysql_fetch_array($thing)) {
		$ind_data[$h_ind['habit_indicator_id']]=$h_ind['habit_id'];
	}
	
	foreach($levels as $id=>$lev) {
		$rlevels[$id]=($i==0?-1:$i);
		$i++;
	}
// 	var_dump($data);
	foreach($data as $key=>$val) {
	if(is_array($val)) {
// 		echo "Thing: '$key' ~".($ind_data[$key])."~ ".($idmap[$ind_data[$key]][$key]['habit_num'])."-".($idmap[$ind_data[$key]][$key]['ind_num'])."=".($rlevels[$val['level_id']]+($val['level_plus']/2))."(".$val['level_id'].")\n";
	$habitdata[$idmap[$ind_data[$key]][$key]['habit_num']][$idmap[$ind_data[$key]][$key]['ind_num']]=$rlevels[$val['level_id']]+($val['level_plus']/2);
	}
	}
// 	var_dump($habitdata);
	echo "</pre>";
	return $habitdata;
}

function getStudentAssessments($studentid, $where="") {
	connectToDB();
	$indeed=mysql_query("SELECT * FROM assessment WHERE `people_id`='" . mysql_real_escape_string($studentid) . "'" . $where);
	echo mysql_error();
	$array=array();
	while($ass=mysql_fetch_array($indeed)) {
		$array[$ass['assessment_id']]=getAssessment($ass['assessment_id']);
	}
	return $array;
}

function getAssessment($id) {
	connectToDB();
	$indeed=mysql_query("SELECT * FROM assessment WHERE assessment_id='" . mysql_real_escape_string($id) . "'");
	$ass=mysql_fetch_array($indeed);
	$array=$ass;
	$array['values']=array();
	$array['habits']=array();
	$thing=mysql_query("SELECT * FROM `assessment_field_value` WHERE `assessment_id`='" . mysql_real_escape_string($ass['assessment_id']) . "'");
	while($assval=mysql_fetch_array($thing)) {
		$array['values'][$assval['assessment_field_id']]=$assval;
	}
	$thing=mysql_query("SELECT * FROM `assessment_habit` WHERE `assessment_id`='" . mysql_real_escape_string($ass['assessment_id']) . "'");
	while($assval=mysql_fetch_array($thing)) {
		$array['habits'][$assval['habit_indicator_id']]=$assval;
	}
	return $array;
}

function canAccessAssessment($people_id,$assid) {
	connectToDB();
	if(isAdvisor($people_id))
		return true;
	$rawthing=mysql_query("SELECT `people_id` FROM `assessment` WHERE `assessment_id`='" . mysql_real_escape_string($assid) . "'");
	echo mysql_error();
	$thing=mysql_fetch_array($rawthing);
	echo mysql_error();
	if($people_id==$thing['people_id'])
		return true;
	return false;
}









